/*
 * protocol.h
 *
 *  Created on: Dec 3, 2012
 *      Author: root
 */

#include "tutorfs.h"

int request_protocol(byte req_type, void *req_ptcl, int req_ptcl_len,
		void *res_ptcl, int res_ptcl_len, struct socket *sock) {
	if (sock != 0) {
		if (send_buffer(sock, &req_type, sizeof(req_type)) < 0)
			return -1;
		if (send_buffer(sock, req_ptcl, req_ptcl_len) < 0)
			return -1;
		if (recv_buffer(sock, res_ptcl, res_ptcl_len) < 0)
			return -1;

		return 0;
	}

	return -1;
}

//#define TYPE_LS 0x01
//#define TYPE_WRITE 0x02
//#define TYPE_READ 0x03
//#define TYPE_MKDIR 0x04
//
//#define TYPE_REQ 0x10
//#define TYPE_RES 0x20
int protocol_length(int ptcl_type) {
	switch (ptcl_type) {
	case TYPE_REQ_LS:
		return sizeof(struct req_ls);
	case TYPE_REQ_WRITE:
		return sizeof(struct req_write);
	case TYPE_REQ_READ:
		return sizeof(struct req_read);
	case TYPE_RES_READ:
		return sizeof(struct res_read);
	case TYPE_REQ_MKDIR:
		return sizeof(struct req_mkdir);
	case TYPE_RES_OK:
		return sizeof(struct res_ok);
	}

	return -1;
}
